home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpcl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  10.2 KB  |  382 lines

  1. /* Copyright (C) 1992, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevpcl.c,v 1.4 2000/09/19 19:00:16 lpd Exp $ */
  20. /* Utilities for PCL printers */
  21. #include "gdevprn.h"
  22. #include "gdevpcl.h"
  23.  
  24. /* ------ Get paper size ------ */
  25.  
  26. /* Get the paper size code, based on width and height. */
  27. int
  28. gdev_pcl_paper_size(gx_device * dev)
  29. {
  30.     float height_inches = dev->height / dev->y_pixels_per_inch;
  31.  
  32.     return
  33.     (height_inches >= 33.2 ? PAPER_SIZE_A0 :
  34.      height_inches >= 23.5 ? PAPER_SIZE_A1 :
  35.      height_inches >= 17.1 ? PAPER_SIZE_A2 :
  36.      height_inches >= 16.6 ? PAPER_SIZE_LEDGER : /* see gdevpcl.h */
  37.      height_inches >= 14.1 ? PAPER_SIZE_A3 :
  38.      height_inches >= 11.8 ? PAPER_SIZE_LEGAL :
  39.      height_inches >= 11.1 ? PAPER_SIZE_A4 :
  40.      PAPER_SIZE_LETTER);
  41. }
  42.  
  43. /* ------ Color mapping ------ */
  44.  
  45. /* The PaintJet and DeskJet 500C use additive colors in separate planes. */
  46. /* We only keep one bit of color, with 1 = R, 2 = G, 4 = B. */
  47. /* Because the buffering routines assume 0 = white, */
  48. /* we complement all the color components. */
  49. #define cv_shift (sizeof(gx_color_value) * 8 - 1)
  50.  
  51. /* Map an RGB color to a printer color. */
  52. gx_color_index
  53. gdev_pcl_3bit_map_rgb_color(gx_device * dev,
  54.                gx_color_value r, gx_color_value g, gx_color_value b)
  55. {
  56.     return (((b >> cv_shift) << 2) + ((g >> cv_shift) << 1) + (r >> cv_shift)) ^ 7;
  57. }
  58.  
  59. /* Map the printer color back to RGB. */
  60. int
  61. gdev_pcl_3bit_map_color_rgb(gx_device * dev, gx_color_index color,
  62.                 gx_color_value prgb[3])
  63. {
  64.     ushort cc = (ushort) color ^ 7;
  65.  
  66.     prgb[0] = -(cc & 1);
  67.     prgb[1] = -((cc >> 1) & 1);
  68.     prgb[2] = -(cc >> 2);
  69.     return 0;
  70. }
  71.  
  72. /* ------ Compression ------ */
  73.  
  74. /*
  75.  * Mode 2 Row compression routine for the HP DeskJet & LaserJet IIp.
  76.  * Compresses data from row up to end_row, storing the result
  77.  * starting at compressed.  Returns the number of bytes stored.
  78.  * Runs of K<=127 literal bytes are encoded as K-1 followed by
  79.  * the bytes; runs of 2<=K<=127 identical bytes are encoded as
  80.  * 257-K followed by the byte.
  81.  * In the worst case, the result is N+(N/127)+1 bytes long,
  82.  * where N is the original byte count (end_row - row).
  83.  * To speed up the search, we examine an entire word at a time.
  84.  * We will miss a few blocks of identical bytes; tant pis.
  85.  */
  86. int
  87. gdev_pcl_mode2compress_padded(const word * row, const word * end_row,
  88.                   byte * compressed, bool pad)
  89. {
  90.     register const word *exam = row;    /* word being examined in the row to compress */
  91.     register byte *cptr = compressed;    /* output pointer into compressed bytes */
  92.  
  93.     while (exam < end_row) {    /* Search ahead in the input looking for a run */
  94.     /* of at least 4 identical bytes. */
  95.     const byte *compr = (const byte *)exam;
  96.     const byte *end_dis;
  97.     const word *next;
  98.     register word test = *exam;
  99.  
  100.     while (((test << 8) ^ test) > 0xff) {
  101.         if (++exam >= end_row)
  102.         break;
  103.         test = *exam;
  104.     }
  105.  
  106.     /* Find out how long the run is */
  107.     end_dis = (const byte *)exam;
  108.     if (exam == end_row) {    /* no run */
  109.         /* See if any of the last 3 "dissimilar" bytes are 0. */
  110.         if (!pad && end_dis > compr && end_dis[-1] == 0) {
  111.         if (end_dis[-2] != 0)
  112.             end_dis--;
  113.         else if (end_dis[-3] != 0)
  114.             end_dis -= 2;
  115.         else
  116.             end_dis -= 3;
  117.         }
  118.         next = --end_row;
  119.     } else {
  120.         next = exam + 1;
  121.         while (next < end_row && *next == test)
  122.         next++;
  123.         /* See if any of the last 3 "dissimilar" bytes */
  124.         /* are the same as the repeated byte. */
  125.         if (end_dis > compr && end_dis[-1] == (byte) test) {
  126.         if (end_dis[-2] != (byte) test)
  127.             end_dis--;
  128.         else if (end_dis[-3] != (byte) test)
  129.             end_dis -= 2;
  130.         else
  131.             end_dis -= 3;
  132.         }
  133.     }
  134.  
  135.     /* Now [compr..end_dis) should be encoded as dissimilar, */
  136.     /* and [end_dis..next) should be encoded as similar. */
  137.     /* Note that either of these ranges may be empty. */
  138.  
  139.     for (;;) {        /* Encode up to 127 dissimilar bytes */
  140.         uint count = end_dis - compr;    /* uint for faster switch */
  141.  
  142.         switch (count) {    /* Use memcpy only if it's worthwhile. */
  143.         case 6:
  144.             cptr[6] = compr[5];
  145.         case 5:
  146.             cptr[5] = compr[4];
  147.         case 4:
  148.             cptr[4] = compr[3];
  149.         case 3:
  150.             cptr[3] = compr[2];
  151.         case 2:
  152.             cptr[2] = compr[1];
  153.         case 1:
  154.             cptr[1] = compr[0];
  155.             *cptr = count - 1;
  156.             cptr += count + 1;
  157.         case 0:    /* all done */
  158.             break;
  159.         default:
  160.             if (count > 127)
  161.             count = 127;
  162.             *cptr++ = count - 1;
  163.             memcpy(cptr, compr, count);
  164.             cptr += count, compr += count;
  165.             continue;
  166.         }
  167.         break;
  168.     }
  169.  
  170.     {            /* Encode up to 127 similar bytes. */
  171.         /* Note that count may be <0 at end of row. */
  172.         int count = (const byte *)next - end_dis;
  173.  
  174.         while (count > 0) {
  175.         int this = (count > 127 ? 127 : count);
  176.  
  177.         *cptr++ = 257 - this;
  178.         *cptr++ = (byte) test;
  179.         count -= this;
  180.         }
  181.         exam = next;
  182.     }
  183.     }
  184.     return (cptr - compressed);
  185. }
  186. int
  187. gdev_pcl_mode2compress(const word * row, const word * end_row,
  188.                byte * compressed)
  189. {
  190.     return gdev_pcl_mode2compress_padded(row, end_row, compressed, false);
  191. }
  192.  
  193. /*
  194.  * Mode 3 compression routine for the HP LaserJet III family.
  195.  * Compresses bytecount bytes starting at current, storing the result
  196.  * in compressed, comparing against and updating previous.
  197.  * Returns the number of bytes stored.  In the worst case,
  198.  * the number of bytes is bytecount+(bytecount/8)+1.
  199.  */
  200. int
  201. gdev_pcl_mode3compress(int bytecount, const byte * current, byte * previous, byte * compressed)
  202. {
  203.     register const byte *cur = current;
  204.     register byte *prev = previous;
  205.     register byte *out = compressed;
  206.     const byte *end = current + bytecount;
  207.  
  208.     while (cur < end) {        /* Detect a maximum run of unchanged bytes. */
  209.     const byte *run = cur;
  210.     register const byte *diff;
  211.     const byte *stop;
  212.     int offset, cbyte;
  213.  
  214.     while (cur < end && *cur == *prev) {
  215.         cur++, prev++;
  216.     }
  217.     if (cur == end)
  218.         break;        /* rest of row is unchanged */
  219.     /* Detect a run of up to 8 changed bytes. */
  220.     /* We know that *cur != *prev. */
  221.     diff = cur;
  222.     stop = (end - cur > 8 ? cur + 8 : end);
  223.     do {
  224.         *prev++ = *cur++;
  225.     }
  226.     while (cur < stop && *cur != *prev);
  227.     /* Now [run..diff) are unchanged, and */
  228.     /* [diff..cur) are changed. */
  229.     /* Generate the command byte(s). */
  230.     offset = diff - run;
  231.     cbyte = (cur - diff - 1) << 5;
  232.     if (offset < 31)
  233.         *out++ = cbyte + offset;
  234.     else {
  235.         *out++ = cbyte + 31;
  236.         offset -= 31;
  237.         while (offset >= 255)
  238.         *out++ = 255, offset -= 255;
  239.         *out++ = offset;
  240.     }
  241.     /* Copy the changed data. */
  242.     while (diff < cur)
  243.         *out++ = *diff++;
  244.     }
  245.     return out - compressed;
  246. }
  247.  
  248. /*
  249.  * Mode 9 2D compression for the HP DeskJets . This mode can give
  250.  * very good compression ratios, especially if there are areas of flat
  251.  * colour (or blank areas), and so is 'highly recommended' for colour
  252.  * printing in particular because of the very large amounts of data which
  253.  * can be generated
  254.  */
  255. int
  256. gdev_pcl_mode9compress(int bytecount, const byte *current,
  257.                const byte *previous, byte *compressed)
  258. {
  259.     register const byte *cur = current;
  260.     register const byte *prev = previous;
  261.     register byte *out = compressed;
  262.     const byte *end = current + bytecount;
  263.  
  264.     while (cur < end) {        /* Detect a run of unchanged bytes. */
  265.     const byte *run = cur;
  266.     register const byte *diff;
  267.     int offset;
  268.  
  269.     while (cur < end && *cur == *prev) {
  270.         cur++, prev++;
  271.     }
  272.     if (cur == end)
  273.         break;        /* rest of row is unchanged */
  274.     /* Detect a run of changed bytes. */
  275.     /* We know that *cur != *prev. */
  276.     diff = cur;
  277.     do {
  278.         prev++;
  279.         cur++;
  280.     }
  281.     while (cur < end && *cur != *prev);
  282.     /* Now [run..diff) are unchanged, and */
  283.     /* [diff..cur) are changed. */
  284.     offset = diff - run;
  285.     {
  286.         const byte *stop_test = cur - 4;
  287.         int dissimilar, similar;
  288.  
  289.         while (diff < cur) {
  290.         const byte *compr = diff;
  291.         const byte *next;    /* end of run */
  292.         byte value = 0;
  293.  
  294.         while (diff <= stop_test &&
  295.                ((value = *diff) != diff[1] ||
  296.             value != diff[2] ||
  297.             value != diff[3]))
  298.             diff++;
  299.  
  300.         /* Find out how long the run is */
  301.         if (diff > stop_test)    /* no run */
  302.             next = diff = cur;
  303.         else {
  304.             next = diff + 4;
  305.             while (next < cur && *next == value)
  306.             next++;
  307.         }
  308.  
  309. #define MAXOFFSETU 15
  310. #define MAXCOUNTU 7
  311.         /* output 'dissimilar' bytes, uncompressed */
  312.         if ((dissimilar = diff - compr)) {
  313.             int temp, i;
  314.  
  315.             if ((temp = --dissimilar) > MAXCOUNTU)
  316.             temp = MAXCOUNTU;
  317.             if (offset < MAXOFFSETU)
  318.             *out++ = (offset << 3) | (byte) temp;
  319.             else {
  320.             *out++ = (MAXOFFSETU << 3) | (byte) temp;
  321.             offset -= MAXOFFSETU;
  322.             while (offset >= 255) {
  323.                 *out++ = 255;
  324.                 offset -= 255;
  325.             }
  326.             *out++ = offset;
  327.             }
  328.             if (temp == MAXCOUNTU) {
  329.             temp = dissimilar - MAXCOUNTU;
  330.             while (temp >= 255) {
  331.                 *out++ = 255;
  332.                 temp -= 255;
  333.             }
  334.             *out++ = (byte) temp;
  335.             }
  336.             for (i = 0; i <= dissimilar; i++)
  337.             *out++ = *compr++;
  338.             offset = 0;
  339.         }        /* end uncompressed */
  340. #undef MAXOFFSETU
  341. #undef MAXCOUNTU
  342.  
  343. #define MAXOFFSETC 3
  344. #define MAXCOUNTC 31
  345.         /* output 'similar' bytes, run-length encoded */
  346.         if ((similar = next - diff)) {
  347.             int temp;
  348.  
  349.             if ((temp = (similar -= 2)) > MAXCOUNTC)
  350.             temp = MAXCOUNTC;
  351.             if (offset < MAXOFFSETC)
  352.             *out++ = 0x80 | (offset << 5) | (byte) temp;
  353.             else {
  354.             *out++ = 0x80 | (MAXOFFSETC << 5) | (byte) temp;
  355.             offset -= MAXOFFSETC;
  356.             while (offset >= 255) {
  357.                 *out++ = 255;
  358.                 offset -= 255;
  359.             }
  360.             *out++ = offset;
  361.             }
  362.             if (temp == MAXCOUNTC) {
  363.             temp = similar - MAXCOUNTC;
  364.             while (temp >= 255) {
  365.                 *out++ = 255;
  366.                 temp -= 255;
  367.             }
  368.             *out++ = (byte) temp;
  369.             }
  370.             *out++ = value;
  371.             offset = 0;
  372.         }        /* end compressed */
  373. #undef MAXOFFSETC
  374. #undef MAXCOUNTC
  375.  
  376.         diff = next;
  377.         }
  378.     }
  379.     }
  380.     return out - compressed;
  381. }
  382.